package cn.itsource.test.sand.demo;

import java.util.ArrayList;

public class FibonacciSeries {

    public static Long fibonacci(int n, ArrayList<Long> arrayList) {
        if (arrayList.size()==0 || n==0){
            return -1L;
        }
        Long num = 0L;
        Long result = -1L;
        boolean flag = true;
        while (flag){
            if (arrayList.size() > n){
                flag = false;
                result = arrayList.get(n);
            }else {
                num = arrayList.get(arrayList.size()-1) + arrayList.get(arrayList.size()-2);
                arrayList.add(num);
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int n = 99; // 要求返回第n个数据，这里以n=6为例
        ArrayList<Long> arrayList = new ArrayList<>();
        arrayList.add(1L);
        arrayList.add(1L);
        Long result = fibonacci(n,arrayList);
        System.out.println("第" + n + "个数据是：" + result);
    }
}
